package com.wpre.vampiremq.mqserver.datacenter;

import com.wpre.vampiremq.VampireMqApplication;
import com.wpre.vampiremq.mqserver.MetaMapper;
import com.wpre.vampiremq.mqserver.core.Binding;
import com.wpre.vampiremq.mqserver.core.Exchange;
import com.wpre.vampiremq.mqserver.core.ExchangeType;
import com.wpre.vampiremq.mqserver.core.MSGQueue;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.File;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: LianBao
 * Date: 2025-01-12
 * Time: 17:22
 */
@Slf4j
public class DataBaseManager {

    private MetaMapper metaMapper;

    //数据库初始化(建库建表,插入默认数据)
    public void init() {
        //从Spring中获取到metaMapper
        metaMapper = VampireMqApplication.context.getBean(MetaMapper.class);
        if (!checkDBExists()) {
            //如果数据库不存在,进行建库建表操作

            //创建库
            File dataDir = new File("./data");
            boolean result = dataDir.mkdirs();

            //创建表
            createTable();
            createDefaultData();
        } else {
            //数据库已经存在
            log.info("数据库已经存在!!!");
        }

    }

    //插入默认数据
    private void createDefaultData() {
        //创建默认的交换机,RabbitMQ中有一个匿名的交换机,类型是DIRECT
        Exchange exchange = new Exchange();
        exchange.setName("");
        exchange.setType(ExchangeType.DIRECT);
        exchange.setDurable(true);
        exchange.setAutoDelete(false);
        metaMapper.insertExchange(exchange);
        log.info("创建初始数据成功!!!");
    }

    //建表操作,不需要创建数据库(meta.db),
    private void createTable() {
        metaMapper.createExchangeTable();
        metaMapper.createQueueTable();
        metaMapper.createBindingTable();
        log.info("建表成功!!!");
    }

    public void deleteDB() {
        File file = new File("./data/meta.db");

        //删除文件
        boolean delete = file.delete();
        if (delete) {
            log.info("删除数据库成功");
        } else {
            log.error("删除数据库失败");
        }

        File dataDir = new File("./data");
        //删除目录
        boolean ret = dataDir.delete();
        if (ret) {
            log.info("删除目录成功");
        } else {
            log.error("删除目录失败");
        }
    }

    //检查数据库是否存在
    private boolean checkDBExists() {
        File file = new File("./data/meta.db");
        return file.exists();
    }

    //其他数据库操作也进行封装
    public void insertExchange(Exchange exchange) {
        metaMapper.insertExchange(exchange);
    }

    public void deleteExchange(String exchangeName) {
        metaMapper.deleteExchange(exchangeName);
    }

    public void insertQueue(MSGQueue queueName) {
        metaMapper.insertQueue(queueName);
    }

    public void deleteQueue(String queueName) {
        metaMapper.deleteQueue(queueName);
    }

    public void insertBinding(Binding binding) {
        metaMapper.insertBinding(binding);
    }

    public void deleteBinding(Binding binding) {
        metaMapper.deleteBinding(binding);
    }

    public List<Exchange> selectAllExchanges() {
        return metaMapper.selectAllExchanges();
    }

    public List<MSGQueue> selectAllQueues() {
        return metaMapper.selectAllQueues();
    }

    public List<Binding> selectAllBindings() {
        return metaMapper.selectAllBindings();
    }

}
